// pages/gr/gr.js
import http from "../../request/index"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    img: "https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0",
    name: "微信用户",
    code: "",
    like: 0

  },
  like() {
    let token = wx.getStorageSync('token')
    if (token == "") {
      wx.showToast({
        title: '请您先登录！',
        icon: "none"
      })
    } else {
      wx.navigateTo({
        url: '../live/live',
      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.user() //获取用户名的数据

  },
  user() {
    let token = wx.getStorageSync('token')
    if (token != "") {
      let obj = wx.getStorageSync('user')
      let code = wx.getStorageSync('code')
      let like = wx.getStorageSync('like')
      this.setData({
        name: obj.name,
        img: obj.img,
        code: code,
        like: like.length
      })
    }
  },
  yi() {
    wx.navigateTo({
      url: '../feedback/index',
    })
  },
  din(e) {
    let token = wx.getStorageSync('token')
    if (token == "") {
      wx.showToast({
        title: '请您先登录！',
        icon: "none"
      })
    } else {
      wx.navigateTo({
        url: '../dinlist/dinlist?index=' + e.currentTarget.dataset.index,
      })
    }
  },

  getUserProfile(e) {
    var flag = true
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
    // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    let token = wx.getStorageSync('token')
    if (token == "" && flag == true) {
      flag = false
      wx.getUserProfile({
        desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {
          this.login(res)
          this.user()
          let like = wx.getStorageSync('like')

          let obj = {
            name: res.userInfo.nickName,
            img: res.userInfo.avatarUrl,

          }
          this.setData({
            name: res.userInfo.nickName,
            img: res.userInfo.avatarUrl,
            like: like.length
          })
          wx.setStorageSync('user', obj)
        }
      })
      wx.login({
        timeout: 5000,
        success: (res => {
          this.setData({
            code: res.code
          })
          wx.setStorageSync('code', res.code)
        })
      })
    } else {
      flag = true
      wx.showModal({
        cancelColor: 'cancelColor',
        showCancel: true,
        title: "您确定要退出吗?",
        confirmText: "确认退出",
        cancelColor: "#000",
        success: (res => {
          if (res.confirm) {
            wx.removeStorageSync('token')
            wx.removeStorageSync('user')
            wx.removeStorageSync('code')
            wx.removeStorageSync('addres')
            wx.reLaunch({
              url: '../gr/gr',
            })
            wx.showToast({
              title: '退出登录成功',
              icon: "none",
            })
            this.user()

          }
          if (res.cancel) {
            wx.showToast({
              title: '取消退出',
              icon: "none",
            })
          }
        })
      })
    }

  },

  //登录接口
  login(e) {
    http("public/v1/users/wxlogin", {
      method: "post",
      data: {
        encryptedData: e.encryptedData,
        rawData: e.rawData,
        iv: e.iv,
        code: this.data.code
      }
    }).then(res => {
      let {
        data
      } = res
      wx.setStorageSync('token', "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo")
      wx.showToast({
        title: '登录成功',
        icon: "success"
      })
      let lo = wx.getStorageSync('long')
      if (lo != "") {
        wx.switchTab({
          url: '../gwc/gwc',
          success: (res => {
            wx.removeStorageSync('long')
          })
        })
      }

    })
  },




  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.user() //获取用户名的数据

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.user() //获取用户名的数据

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})